import router from "@/router";
import { Toast, Dialog } from "vant";
import { reds, redsbalance,redladders,redregister} from "@/server/red";
import { setStorage, setSession, removeStorage } from "@/utils/common";

export default {
  state: () => ({
    teamdescription: "",
    repit_id:''
  }),
  mutations: {
    setteamdescription(state, data) {
      state.teamdescription = data;
    },
    setrepit_id(state,data){
      state.repit_id = data
    }
  },
  actions: {
    reds({ commit }, data) {
      return new Promise((resolve, reject) => {
        reds(data).then((res) => {
          if (res.code == 0) {
            commit("setrepit_id",res.data.red.pet_id)
            resolve(res.data);
          }
        });
      });
    },
    redsbalance({ commit,state}, data) {
      return new Promise((resolve, reject) => {
        redsbalance(data).then((res) => {
          if (res.code == 0) {
            resolve(true);
          } else if (res.code == 40002) {
            resolve(false);
            Dialog.confirm({
              title: "Tips",
              message: res.msg,
              confirmButtonText: "To bind",
              cancelButtonText: "Cancel",
            })
              .then(() => {
                router.push("/bankcard");
              })
              .catch(() => {
                // on cancel
              });
          } else if (res.code == 80002) {
            Dialog.confirm({
              title: "Tips",
              message: res.msg,
              confirmButtonText: "To Invest",
              cancelButtonText: "Cancel",
            })
              .then(() => {
                router.push(`/market`);
              })
              .catch(() => {
                // on cancel
              });
          } else if (res.code == 80003) {
            Toast(res.msg);
            // Dialog.confirm({
            //   title: "Tips",
            //   message: res.msg
            // })
            //   .then(() => {
            //     router.push("/market");
            //   })
            //   .catch(() => {
            //     // on cancel
            //   });
            resolve(false);
          }else{
            Toast(res.msg)
            resolve(false);
          }
        });
      });
    },
    redladders({ commit }, data) {
      return new Promise((resolve, reject) => {
        redladders(data).then((res) => {
          if (res.code == 0) {
            resolve(res.data);
          }
        });
      });
    },
    redregister({ commit }, data) {
      return new Promise((resolve, reject) => {
        redregister(data).then((res) => {
          if (res.code == 0) {
            commit("setrepit_id",res.data.red.pet_id)
            resolve(res.data);
          }
        });
      });
    },
    
  },
  getters: {},
};
